<?php
/**
 * 认证用户后台登录
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Front;

use Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\AuthenticationUserService\AuthenticationUserService;
use App\Services\ProductsService\ProductsService;


class AuthController extends Controller
{
    protected $auth;
    protected $product;

    public function __construct(AuthenticationUserService $authentication, ProductsService $product)
    {
        $this->auth = $authentication;
        $this->product = $product;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 登录
     */
    public function loginIndex(Request $request)
    {
        $userinfo = $request->session()->get('userinfo');
        if ($userinfo != null) {
            return redirect('/authentication/home');
        } else {
            return view('Front/login');
        }
    }

    /**
     * @param Request $request
     * @return mixed
     * 发送验证码
     */
    public function sendCaptcha(Request $request)
    {
        $result = $this->auth->sendCaptcha($request);
        return Response::json($result);
    }

    /**
     * @param Request $request
     * @return mixed
     * 登录
     */
    public function login(Request $request)
    {
        $result = $this->auth->authenticationLogin($request);
        return Response::json($result);
    }

    /**
     * 注销
     */
    public function logout(Request $request)
    {
        $request->session()->forget('user_type');
        $request->session()->forget('user_id');
        $request->session()->forget('userinfo');
        return redirect('authentication/login');
    }

    public function home(Request $request)
    {
        $userinfo = $request->session()->get('userinfo');
        if ($userinfo == null) {
            return redirect('authentication/login');
        } else {
            return view('Front/layout/master2');
        }
    }

    public function person(Request $request)
    {
        $user_id = $request->session()->get('user_id');
        $data = $this->auth->userCenter($user_id);
        $list = $data['list'];
        $userinfo = $data['userinfo'];
        return view('Front/index')->with(['lists' => $list, 'userinfo' => $userinfo]);
    }
}